package com.itttq.gshop.generic.controller;

import java.beans.PropertyEditorSupport;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.domain.po.BasePo;
import com.itttq.gshop.domain.po.UserInfoPo;
import com.itttq.gshop.domain.to.PageParamTo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.util.Constants;

public abstract class GenericController implements Serializable {
	private static final long serialVersionUID = -8751733063972470283L;

	/**
	 * 处理接收参数为""时转换为date类型校验不通过的问题
	 * 
	 * @param dataBinder
	 * @Author 迷蝶
	 * @return void
	 */
	@InitBinder  
	public void InitBinder(WebDataBinder dataBinder) {
		dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
			public void setAsText(String value) {
				if(null == value) {
					setValue(null);
					return;
				}
				String formatStr = "yyyy-MM-dd";
				if(value.contains(" ")) {
					formatStr = "yyyy-MM-dd hh:mm:ss";
				}
				try {
					setValue(new SimpleDateFormat(formatStr).parse(value));
				} catch (ParseException e) {
					setValue(null);
				}
			}
			public String getAsText() {
				return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format((Date) getValue());
			}
		});
	}
	
	/**
	 * 从request中获取httpSession
	 * 
	 * @param request HttpServletRequest请求
	 * @return HttpSession对象
	 */
	public HttpSession getSession(HttpServletRequest request) {
		return request.getSession();
	}

	/**
	 * 设置值到session中
	 * 
	 * @param request request请求
	 * @param name session中名称
	 * @param value session中值
	 */
	public void setSession(HttpServletRequest request, String name, Object value) {
		this.getSession(request).setAttribute(name, value);
	}

	/**
	 * 设置值到session中
	 * 
	 * @param request request请求
	 * @param name session中名称
	 */
	public Object getSession(HttpServletRequest request, String name) {
		return null == this.getSession(request) ? null : this.getSession(request).getAttribute(name);
	}

	/**
	 * 获取分页参数，返回分页的Map对象
	 *
	 * @param request
	 * @return
	 * @auther <a href="mailto:gaoxvguo060@haopinfeng.com">高绪国</a> 2014-8-24
	 *         下午10:14:40
	 */
	public PageParamTo getPageParam(HttpServletRequest request) {
		String pageSizeStr = request.getParameter("ps");
		String pnStr = request.getParameter("pn");
		Integer ps = null,pn = null;
		if (!StringUtils.isEmpty(pageSizeStr)) {
			ps = Integer.parseInt(pageSizeStr);
		}
		if (!StringUtils.isEmpty(pnStr)) {
			pn = Integer.parseInt(pnStr);
		} else {
			pn = 1;
		}
		return getPageParam(pn, ps);
	}

	public PageParamTo getPageParam(Integer pn, Integer ps) {
		// String pageSizeStr = request.getParameter("ps");
		int start = 0;
		if (null == ps) {
			// 默认分页15页
			ps = 15;
		} else if (ps > 50) {
			ps = 50;
		}
		if (null != pn && pn.intValue() > 0) {
			start = (pn - 1) * ps;
		} else {
			pn = 1;
		}
		PageParamTo ppt = new PageParamTo();
		 ppt.setNo(pn);
		 ppt.setSize(ps);
		 ppt.setStart(start);
		return ppt;
	}

	public PageParamTo getPpt(int size) {
		PageParamTo ppt = new PageParamTo();
		ppt.setSize(size);
		return ppt;
	}
	/**
	 * 获取分页并设置到PO类里面
	 * 
	 * @param request
	 * @param bp
	 * @Author 迷蝶
	 */
	public void getPpt(HttpServletRequest request, BasePo bp) {
		PageParamTo ppt = this.getPageParam(request);
		bp.setStart(ppt.getStart());
		bp.setSize(ppt.getSize());
	}
	
	public ModelAndView getPageMv(String attrName, 
			ModelAndView mv, ResultPageTo<?> rpt) {
		mv.addObject(attrName, rpt.getData());
		mv.addObject("count", rpt.getTotal());
		mv.addObject("start", rpt.getStart());
		mv.addObject("size", rpt.getSize());
		return mv;
	}
	
	public void setPpt(HttpServletRequest request, BasePo po) {
		PageParamTo ppt = null == request ? new PageParamTo() : getPageParam(request);
		po.setStart(ppt.getStart());
		po.setSize(ppt.getSize());
	}
	/**
	 * 获取登录前的URL
	 * @param request
	 * @param url	如果没有登录前的URL时，跳转到此URL
	 * @return
	 */
	public String getLastUrl(HttpServletRequest request, String url) {
		Object lastUrl = this.getSession(request, Constants.SESSION_URL_LAST);
		// 清除上次地址
		this.getSession(request).removeAttribute(Constants.SESSION_URL_LAST);
		return null == lastUrl ? url : lastUrl.toString();
	}
	
	/**
	 * 获取当前登录用户对象
	 * @param request
	 * @return
	 */
	public UserInfoPo getUserFromSession(HttpServletRequest request) {
		Object user = this.getSession(request, Constants.SESSION_USER);
		return null == user ? null :  (UserInfoPo) user;
//		// 测试
//		UserInfoPo user1 = new UserInfoPo();
//		user1.setId(1);
//		user1.setUsername("butterfly");
//		user1.setMemberScore(34211);
//		user1.setMobile("1508****315");
//		user1.setHead("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2825158543,122738554&fm=111&gp=0.jpg");
//		return user1;
	}
	/**
	 * 获取当前登录用户ID
	 * @param request
	 * @return
	 */
	public Integer getUserIdFromSession(HttpServletRequest request) {
		return getUserFromSession(request).getId();
	}
}
